package edu.nf.hotel.admin.impl;

import edu.nf.hotel.admin.AdminLoginService;
import edu.nf.hotel.admin.AdminUser;
import edu.nf.hotel.admindao.AdminUserDao;
import edu.nf.hotel.admindao.impl.AdminUserDaoImpl;
import edu.nf.hotel.exception.LoginException;
import edu.nf.hotel.util.ConnUtils;
import edu.nf.hotel.util.Md5Util;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanListHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2023-06-15
 * @Author 陈
 */
public class AdminLoginServiceImpl implements AdminLoginService {
    @Override
    public AdminUser login(String username, String password) {
        AdminUserDao adminUserDao = new AdminUserDaoImpl();
        AdminUser user = adminUserDao.select(username);
        if (user == null){
            throw new LoginException(500,"此账号没有注册");
        }
//        if (Md5Util.equals(password,user.getAdmPassword())){
//            throw new LoginException(500,"错误的账号或密码");
//        }
        if (!user.getAdmPassword().equals(password)){
            throw new LoginException(500,"错误的账号或密码");
        }
        return user;
    }

    @Override
    public AdminUser quy(int aid) {
        AdminUserDao adminUserDao = new AdminUserDaoImpl();
        AdminUser adminUser = adminUserDao.select(aid);
        if (adminUser != null){
            return adminUser;
        }
        throw new LoginException(500,"查无此人");
    }

    /**
     * 添加管理员
     * @param admUserName
     * @param admPassword
     * @param admSex
     * @param admPhone
     * @return
     */
    @Override
    public AdminUser add(String admUserName, String admPassword, String admSex, String admPhone) {
        AdminUser adminUser = new AdminUser();
        adminUser.setAdmUserName(admUserName);
        adminUser.setAdmPassword(admPassword);
        adminUser.setAdmSex(admSex);
        adminUser.setAdmPhone(admPhone);

        AdminUserDao dao = new AdminUserDaoImpl();
        AdminUser user = dao.select(admUserName);

        if (user == null ){
            //结果集
            dao.insert(adminUser);
        }else {
            System.out.println(user);
            throw new LoginException(500,"账号已经注册，请重新输入用户名");
        }
        return adminUser;
    }

}
